import java.util.Iterator;

public class ResizingArrayStack<Item> implements Iterable<Item>{
    private Item[] array;
    private int N;
    public ResizingArrayStack(int cap){
        array = (Item[]) new Object[cap];
        N = cap;
    }
    public void push(Item a){
        if(isFull()) resize(array.length*2);
        array[N++] = a;
    }
    public Item pop(){
        if(!isEmpty())
        return array[--N];
        else
        return null;
    }
    public boolean isEmpty(){
        return N==0;
    }
    public int size(){
        return N;
    }
    private boolean isFull(){
        return N==array.length;
    }
    private void resize(int cap){
        Item[] tem = (Item[]) new Object[cap];
        for(int i = 0;i< array.length;i++){
            tem[i] = array[i];
        }
        array = tem;
    }

    @Override
    public Iterator<Item> iterator() {
        return new ReverseArrayIterator();
    }
    private class ReverseArrayIterator implements Iterator<Item>{
        private int i = N;
        @Override
        public void remove() {

        }

        @Override
        public boolean hasNext() {
            return i>0;
        }

        @Override
        public Item next() {
           return array[--i];
        }
    }
}
